package com.zh.web;


import com.zh.web.bean.ResponseResult;
import com.zh.web.clients.HelloClient;
import com.zh.web.clients.IdClient;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequiredArgsConstructor
public class TestController {

    //因为是final, 所以要使用构造顺完成注入, 构造器注入与@Autowired注解不能同时使用，
    // 构造器注入相对于@Autowired注解更简洁，推荐使用构造器注入，且在循环依赖时，构造器注入会更加稳定。
    private final HelloClient helloClient;
    private final IdClient idClient;

    //注入RestTemplate客户端对象
    private final RestTemplate restTemplate;

    @GetMapping("/restTemplateTest")
    public ResponseResult restTemplateTest() {
        //ResponseEntity  rr=restTemplate.exchange("http://idGenerator/id/next", HttpMethod.GET, null, ResponseResult.class);
        //ResponseResult rr=restTemplate.getForObject("http://169.254.43.53:8181/id/next", ResponseResult.class );
        ResponseResult rr=restTemplate.getForObject("http://idGenerator/id/next", ResponseResult.class );

        return rr;
    }

    @GetMapping("/test")
    public ResponseResult test() {
        ResponseResult responseResult = helloClient.getHello();           // 调用 hello-service
        ResponseResult responseResult1= idClient.getNextId();                     // 调用 id-generator
        String result = responseResult.getData().toString() + "   " + responseResult1.getData().toString();
        return ResponseResult.builder()
                .code(1)
                .msg("")
                .data(result)
                .build();
    }
}
